package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ZhanK
 * @since 2022-02-16
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

  //随便写了下lambda，这个.lambda()方法可以把QueryWrapper改造成LambdaQueryWrapper。
  // 这样就能使用查询的数据库字段::，直接获取实体类对用的数据库字段，省去了去数据库查
    @Override
    public Boolean updateTeacherSelect() {
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setName("zk");
        List<EduTeacher> list = this.list(new QueryWrapper<EduTeacher>().lambda().eq(EduTeacher::getName, "涨三"));
        LambdaQueryWrapper<EduTeacher> wrapper = new QueryWrapper<EduTeacher>().lambda().like(EduTeacher::getName, "Z");
        boolean update = this.update(eduTeacher, wrapper);
        return update;
    }
    //分页查询讲师，返回自己写的分页的page的多个字段属性的map集合
    @Override
    public Map<String, Object> getTeacherPage(Page<EduTeacher> teacherPage) {
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<EduTeacher>().orderByDesc(EduTeacher::getSort);
         baseMapper.selectPage(teacherPage, wrapper);

        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();

        boolean hasNext = teacherPage.hasNext();//上一页
        boolean hasPrevious = teacherPage.hasPrevious();//下一页

        Map<String, Object> map = new HashMap<>();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);


        return map;
    }

}
